﻿using UnityEngine;
using System.Collections;
using System;

public static class CmdParser
{
    public static void OnLogin(Cmd cmd)
    {
        if (!Nets.CheckCmd(cmd, typeof(LoginCmd)))
        {
            return;
        }
        LoginCmd loginCmd = cmd as LoginCmd;


        //验证账号密码
        //找到玩家的存档
        Server.instance.curPlayer = new Player();
        //收到玩家创建的列表
        var player = Server.instance.curPlayer;
        RoleListCmd roleListCmd = new RoleListCmd();

        //roleListCmd.AllRole = player.AllRole.GetRange(0, player.AllRole.Count);
        foreach (var role in player.AllRole)
        {
            var roleInfo = new SelectRoleInfo() { Name = role.Name, ModelID = role.ModelID };
            roleListCmd.AllRole.Add(roleInfo);
        }
        Server.instance.sendCmd(roleListCmd);
    }

    internal static void onSelectRole(Cmd cmd)
    {
        if (!Nets.CheckCmd(cmd, typeof(SelectRoleCmd)))
        {
            return;
        }
        SelectRoleCmd selectRoleCmd = cmd as SelectRoleCmd;

        SelectRoleInfo curRole = Server.instance.curPlayer.AllRole[selectRoleCmd.Index];

        // 告诉客户端，进入哪个场景编号
        var sceneID = 1;
        EnterMapCmd enterMapCmd = new EnterMapCmd() { MapID = sceneID };
        var thisid = RoleServer.GetNewThisID();
        //告诉客户端主角的id
        MainRoleThisidCmd mainRoleThisid = new MainRoleThisidCmd() { ThisID = thisid };

        CreateSceneRole roleCmd = new CreateSceneRole();

        roleCmd.ThisID = thisid;
        roleCmd.Name = curRole.Name;
        roleCmd.ModelID = curRole.ModelID;
        roleCmd.pos = Vector3.zero;
        roleCmd.pos2d = Vector2.zero;
        roleCmd.FaceTo = Vector3.forward;

        Server.instance.sendCmd(enterMapCmd);
        Server.instance.sendCmd(mainRoleThisid);

        Server.instance.sendCmd(roleCmd);

    }
}
